/* Autostop
 *
 * Copyright (c) Maciej Gajewski, Grzegorz Latarowski
 * http://code.google.com/p/autostop-automaty-drogowe
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
 */

#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H

// qt
#include <QThread>
#include <QTcpSocket>

// local
#include "gexception.h"
#include "autoserver.h"


// =============================== thread class ===================
class ServerThread : public QThread
{
private:
	int	_socket;	///< socket handle
	
public:

	// ---------------------- constructor ---------------------------
	ServerThread( int socket, QObject* parent ) : QThread( parent ), _socket( socket )
	{
	}
	virtual ~ServerThread() {}

	// ---------------------------- run --------------------------------
	virtual void run()
	{
		QTcpSocket socket;
		socket.setSocketDescriptor( _socket );
		// say something
		qDebug("Incomming connection from %s", qPrintable( socket.peerAddress().toString() ) );

		try {
			// create server
			AutoServer server( NULL );

			server.setSocket( & socket );

			server.processPackets();
		}
		catch( const GException& e )
		{
			qDebug("TestServer: connection error: %s", qPrintable( e.getMessage() ) );
		}
	}
	
};

#endif // SERVERTHREAD_H


// eof

